package action;

import bean.Department;
import bean.Employee;
import dao.DepartmentDAO;
import dao.DepartmentDAOImpl;
import dao.EmployeeDAO;
import dao.EmployeeDAOImpl;
import tools.HibernateSessionFactory;

import java.util.List;

public class EmployeeAction {
    private DepartmentDAO departmentDAO = new DepartmentDAOImpl(HibernateSessionFactory.getSession());
    private List<Department> departments;
    private EmployeeDAO employeeDAO = new EmployeeDAOImpl(HibernateSessionFactory.getSession());
    private Employee employee;
    private List<Employee> employees;

    public List<Department> getDepartments() {
        return departments;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public String execute() {
        departments = departmentDAO.queryAll();
        employees = employeeDAO.queryAll();
        return "success";
    }

    public String add() {
        return employeeDAO.add(employee) ? "add" : null;
    }

    public String edit() {
        departments = departmentDAO.queryAll();
        employee = employeeDAO.query(employee.getEmployeeId());
        return "edit";
    }

    public String modify() {
        return employeeDAO.modify(employee) ? "modify" : null;
    }

    public String remove() {
        return employeeDAO.remove(employee.getEmployeeId()) ? "remove" : null;
    }
}
